package com.udp.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author Administrator
 * name:tankhuanpei@gmail.com
 * psw:PC9mJ8Av6Es7
 */
public class SendFile {
    
    public static void main(String[] args) {
        
        
    }
    
    public class  sendThread extends Thread{
        
        private Socket mSocket  = null ;
        private OutputStream os = null;
        private InputStream is = null ;
        private FileInputStream fis = null ;
        File file ;
        String ip ;
        public sendThread(File file ,  String ip ) {
            this.file = file ;
            this.ip = ip ;
        }
        
        @Override
        public void run() {
            try {
                mSocket = new Socket(InetAddress.getByName(ip) , ConfigConstant.TCP_CONNECTPORT );
                os = mSocket.getOutputStream();
                fis = new FileInputStream(file);
                
                int filelenth = (int) file.length();
                int left = filelenth;
                int sendOnce = 0 ;
                byte buffer[] = new byte[ConfigConstant.WIFI_BUFFER_LENGTH];
                while(left > 0) {
                    sendOnce = fis.read(buffer);
                    os.write(buffer, 0, sendOnce);
                    os.flush();
                    left = left - sendOnce;
                    Thread.sleep(500);
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    mSocket.close();
                    os.close();
                    is.close();
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        }
        
    }

}
